package mock.servicesdirectory.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.opensymphony.xwork2.ActionSupport;

import mock.servicesdirectory.dao.daointerface.PremiseListDAO;
import mock.servicesdirectory.web.model.PremiseList;

public class PremiseListAction extends ActionSupport{
	// get index row - i.e. user click to sort.

    private String sidx;
    // Search Field
    private String searchField;

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }
    // The Search String
    private String searchString;
    // he Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    private String searchOper;
    // Your Total Pages
    private Integer total = 0;
    //Your result List
    private List<PremiseList> gridModel;
    //Temp list
    private List<PremiseList> myOrganisations;
    //get how many rows we want to have into the grid - rowNum attribute in the grid
    private Integer rows = 0;
    //Get the requested page. By default grid sets this to 1.
    private Integer page = 0;
    // sorting order - asc or desc
    private String sord;
    private boolean loadonce = false;
    public List<PremiseList> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<PremiseList> gridModel) {
    	this.gridModel = gridModel;
    }

    public Integer getPage() {
        return page;
    }

    public boolean isLoadonce() {
        return loadonce;
    }

    public void setLoadonce(boolean loadonce) {
        this.loadonce = loadonce;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRecords() {
        return records;
    }

    public void setRecords(Integer records) {
        this.records = records;
        if (this.records > 0 && this.rows > 0) {
            this.total = (int) Math.ceil((double) this.records / (double) this.rows);
        } else {
            this.total = 0;
        }
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }
    // All Records
    private Integer records = 0;

    public String execute() {
        Logger.getLogger("MyActiontable ").log(Level.INFO, "Loading Table...");

        //Count Rows (select count(*) from custumer)
        setRecords(PremiseListDAO.count());
        int to = (getRows() * getPage());
        int from = to - getRows();

        if (to > getRecords()) {
            to = getRecords();
        }

        myOrganisations = PremiseListDAO.find(from, to);

        if (getSord() != null && getSord().equalsIgnoreCase("asc")) {
            System.out.println("Sort Ascending");
            Collections.sort(myOrganisations);
        }
        if (getSord() != null && getSord().equalsIgnoreCase("desc")) {
            System.out.println("Sort Descending");
            Collections.sort(myOrganisations);
            Collections.reverse(myOrganisations);
        }

        if (searchString != null && searchOper != null && !searchString.equals("") && !searchOper.equals("")) {
            System.out.println("Searching within Database");
            int id = Integer.parseInt(searchString);
            System.out.println("I think a problem here");
            
            if (searchOper.equalsIgnoreCase("eq")) {
                System.out.println("Searching Equality");
                List<PremiseList> cList = new ArrayList<PremiseList>();
                cList.add(PremiseListDAO.findById(id));
                setGridModel(cList);
            } else if (searchOper.equalsIgnoreCase("ne")) {
                System.out.println("Searching NotEqual");
                setGridModel(PremiseListDAO.findNotById(id, from, to));
            } else if (searchOper.equalsIgnoreCase("lt")) {
                System.out.println("Searching LessThan");
                setGridModel(PremiseListDAO.findLesserAsId(id, from, to));
            } else if (searchOper.equalsIgnoreCase("gt")) {
                  System.out.println("Searching GreaterThan");
                setGridModel(PremiseListDAO.findGreaterAsId(id, from, to));
            } else {
                System.out.println("Searching No Operator Found");
                setGridModel(myOrganisations);
            }
        } else {
            System.out.println("Not Searching Anywhere");
            setGridModel(myOrganisations);
        }
        //calculate the total pages for the query
        total = (int) Math.ceil((double) getRecords() / (double) getRows());
        return SUCCESS;
    }

    Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getJSON() {
        return execute();
    }
    
    /**
     * Method used to render List Premise page for menu link
     * @return
     */
    public String listPremises(){
    	return SUCCESS;
    }
}
